<?php
session_start();
//-------
// Includes
if(file_exists('functions.inc.php'))
	include_once('functions.inc.php');
elseif(file_exists('../functions.inc.php'))
	include_once('../functions.inc.php');
else
{
	throw new Exception('Error : Cannot find ROOTDIR/functions.inc.php');
	die();
}

// Check if it's an ajax call -- if not, page manually loaded, exit
if(!isset($_POST) or empty($_POST) or !is_array($_POST) or empty($_POST['action']))
{
	echo('This page must not be loaded manually.');
	die();
}

// What do we do ?
switch($_POST['action'])
{
	// AFFICHAGE (SHOW)
	case 'show':
		// Update notes
		//$xp = new XMLParser();
		//$xp->storeArticles();
		// How many articles by page ?
		$bypage = 10;
		// Get limits
		if(	isset($_POST['start']) 	
			and isset($_POST['limit']) 
			and is_numeric($_POST['start']) 
			and is_numeric($_POST['limit']))
		{
			$start = $_POST['start'];
			$limit = $_POST['limit'];
		}
		else
		{
			$start = 1;
			$limit = $bypage; // default number of notes to load
		}
				
		// Load articles
		$articlesSet = Articles::load($start,$limit); // returns an ArticlesSet object
		include('../views/articles_list.php');
		PageSet::printLoadMore($start+$bypage,$bypage);
	break;
	
	// AFFICHAGE DU TITRE SEUL
	case "show_title":
		if(isset($_POST['id']) and is_numeric($_POST['id']))
		{
			$id = $_POST['id'];
			$article = Articles::load($id);
			include('../views/articles_title.php');
		}
	break;

	// SET CITED
	case 'set_cited':
		if(isset($_POST['id']) and is_numeric($_POST['id']) and isset($_POST['cited']))
		{
			$id = $_POST['id'];
			$cited = $_POST['cited'];
			
			$a = Articles::load($id);
			$a->setCited($cited);
			$a->setDateCited();
			$a->save();			
		}
	break;
	
	// ADD TAG
	case 'add_tag':
		if(isset($_POST['id']) and is_numeric($_POST['id']) and isset($_POST['tag']))
		{
			echo "adding tag";
			$id = $_POST['id'];
			$tag = $_POST['tag'];
			$a = Articles::load($id);
			$a->addTag($tag);
		}
	break;
	
	default: break;
}

?>
